﻿var PrintHouseHold = function () {
    var initArea = function (unitCode) {
        var cbx = $('#frmPrintHouseHold ').find('#area_id');
        cbx.html('');
        if (unitCode && unitCode.length > 0) {
            $.ajax({
                url: actionUrl.getJsonArea + "?UnitCode=" + unitCode,
                dataType: "json",
                success: function (data) {
                    //cbx.html('<option value="">Chọn</option>');
                    var code = "";
                    $(data).each(function () {
                        if (code == "") code = this.AREA_ID;
                        cbx.append("<option value=" + this.AREA_ID + ">" + this.AREA_NAME + "</option>");
                    });
                    if (code.length) cbx.val(code);
                    cbx.change();
                }
            });
        }
    };

    var initTable = function (area_id) {
        $.ajax({
            url: actionUrl.getJsonHouseHold + '?area_id=' + area_id,
            dataType : 'json',
            success: function (data) {
                var table = $($('#divTableTemp2').html());
                var divResult = $('#divResult');
                divResult.html(table);
                table = divResult.find("table");
                var stt = 1;
                $(data).each(function () {
                    var row = $("<tr></tr>");
                    var cellsData = this;
                    var cell = $("<td align='center' class='hidden-480'></td>");
                    cell.html(stt);
                    stt++;
                    row.append(cell);
                    cell = $("<td align='center' class='hidden-480'></td>");
                    cell.html(cellsData.HOUSEHOLD_CODE);
                    row.append(cell);
                    cell = $("<td align='left'></td>");
                    cell.append("<div class='hidden-780'>" + cellsData.HOUSEHOLD_NAME + "</div>");
                    cell.append("<div class='display-780'><b>Tên hộ: " + cellsData.HOUSEHOLD_NAME + "</b></div>");
                    cell.append("<div class='display-480'>Mã hộ: " + cellsData.HOUSEHOLD_CODE + "</div>");
                    cell.append("<div class='display-580'>Email: " + cellsData.HH_HEAD_EMAIL + "</div>");
                    cell.append("<div class='display-680'>Mobile: " + cellsData.HH_HEAD_MOBILE + "</div>");
                    cell.append("<div class='display-780'>Địa chỉ: " + cellsData.ADDRESS + "</div>");
                    row.append(cell);

                    cell = $("<td align='left' class='hidden-580'></td>");
                    cell.html(cellsData.HH_HEAD_EMAIL);
                    row.append(cell);

                    cell = $("<td align='left' class='hidden-680'></td>");
                    cell.html(cellsData.HH_HEAD_MOBILE);
                    row.append(cell);

                    cell = $("<td align='left' class='hidden-780'></td>");
                    cell.html(cellsData.ADDRESS);
                    row.append(cell);

                    cell = $("<td align='center' style=\"float: left;\"></td>");
                    cell.html("<a id='lnkPrint" + cellsData.HOUSEHOLD_ID + "' style='cursor:pointer; white-space:nowrap; text-decoration:none;' onclick='PrintHouseHold.fnPrint(\"" + cellsData.HOUSEHOLD_ID + "\",\"" + cellsData.HOUSEHOLD_CODE + "\");'><i class=\"fa fa-print\" ></i> <span class='hidden-680'>In phiếu (" + cellsData.PRINT_COUNT + ")</span></a>");
                    row.append(cell);

                    table.find("tbody").append(row);
                });

                var oTable = table.dataTable({
                    language: {
                        "lengthMenu": "Hiển thị _MENU_ <span class='hidden-680'>bản ghi.</span>",
                        "zeroRecords": "Không có dữ liệu.",
                        "info": "Hiển thị trang _PAGE_ của _PAGES_ trang.",
                        "infoEmpty": " ",
                        "infoFiltered": "(Kết quả tìm kiếm từ _MAX_ bản ghi)",
                        search: "<span class='hidden-680'>Tìm kiếm:</span>",
                        paginate: {
                            "first": "Trang đầu",
                            "last": "Trang cuối",
                            "next": "Tiếp theo",
                            "previous": "Trước"
                        },
                    },
                    "order": [
                    ],
                    "lengthMenu": [
                        [1, 5, 15, 20, -1],
                        [1, 5, 15, 20, "Tất cả"] 
                    ],
                    "pageLength": 5,
                    "columnDefs": [{  
                        'orderable': false,
                        'targets': [0, 1, 2, 3, 4, 5]
                    }, {
                        "searchable": false,
                        "targets": [0]
                    }],
                });

                var tableWrapper = $('#divResult');
                tableWrapper.find('.dataTables_length select').select2();
                tableWrapper.find('.dataTables_filter input').addClass('input-small').addClass('input-sm').addClass('form-control').css('display','inline').attr('placeholder', 'Tìm kiếm');
            }
        });

    };

    var fnPrint = function (id, code) {
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

        $.ajax({
            url: actionUrl.updatePrint + "?hid=" + id,
            dataType: "json",
            success: function (d) {
                var d = d[0].PRINT_COUNT;
                PrintHouseHold.updateLink(id, d);
            }
        });

        if (is_chrome) {
            var divPrint = $('#frmPrintHouseHold').find('#divPrint');
            divPrint.html('<iframe src="' + actionUrl.print + '?id=' + id + '&code=' + code + '" style="display:none;"></iframe>');
        }
        else {
            window.open(actionUrl.print + '?id=' + id + '&print=0');
        }
        return false;
    };

    return{
        init: function () {
            initArea(actionUrl.data.xa_code);
            var cbx = $('#frmPrintHouseHold').find('#area_id');
            cbx.change(function () {
                var me = $(this);
                initTable(me.val());
            });

            $('#btnPrintPhieuTrang_Ho').click(function () {
                var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

                if (is_chrome) {
                    var divPrint = $('#frmPrintHouseHold').find('#divPrint');
                    divPrint.html('<iframe src="' + actionUrl.print + '?id=0&code=" style="display:none;"></iframe>');
                }
                else {
                    window.open(actionUrl.print + '?id=0&print=0');
                }

                return false;
            });


            $('#btnPrintPhieuTrang_CD').click(function () {
                var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

                if (is_chrome) {
                    var divPrint = $('#frmPrintHouseHold').find('#divPrint');
                    divPrint.html('<iframe src="' + actionUrl.printResident0 + '" style="display:none;"></iframe>');
                }
                else {
                    window.open(actionUrl.printResident0 + '?print=0');
                }
                
                return false;
            });

        },
        fnPrint: fnPrint,
        updateLink: function (hid, i) {
            $('#lnkPrint' + hid).html("<i class=\"fa fa-print\" style=\"float: left;\"></i> <span class='hidden-680'>In phiếu (" + i + ")</span>");
        }
    };

}();